package vcp.controllers;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import db.parking.beans.Complaints;
import vcp.common.AbstractController;
import vcp.common.IController;
import vcp.gui.components.VCPPanel;
import vcp.gui.components.VCPScrollTable;
import vcp.gui.panels.customerservice.PnlResolveComplaints;
import vcp.models.ResolveComplaintsModel;
import vcp.views.ResolveComplaintsView;

/**
 * Resolve Complaints Controller
 *
 */
public class ResolveComplaintsController extends AbstractController implements
	ActionListener,AncestorListener,ListSelectionListener,TableModelListener, IController {
	ResolveComplaintsModel model;
	ResolveComplaintsView view;
	
	@Override
	public void addModel(Object model) {
		this.model = (ResolveComplaintsModel) model;
	}

	@Override
	public void addView(Object view) {
		this.view = (ResolveComplaintsView) view;	
	}
    
	

	@Override
	public void actionPerformed(ActionEvent arg) {
		 String cmd = arg.getActionCommand();
		 if(cmd.equals("BtnCompensate")){
			 JTextField amount = this.view.getComplaintsForm().getTextCompensate();
		     if(amount.getText().isEmpty()){ // do validation checks
		    	 JOptionPane.showMessageDialog(null,
		    			    "Amount field is empty.",
		    			    "Error",
		    			    JOptionPane.ERROR_MESSAGE);
		     }else{
		    	 Complaints comp = new Complaints();
		    	 comp.setComplaintID(this.view.getComplaintsForm().getCompID());
		    	 comp.setAmount(Float.parseFloat(amount.getText()));
		    	 model.updateResolveAmount(comp);
		     }
		 }
	}

	@Override
	public void ancestorAdded(AncestorEvent arg) {
		//System.out.println(arg.getAncestor());
		if(this.view.getComplaintsForm().getComplaintPane().isVisible())
		    this.view.getComplaintsForm().getComplaintPane().setVisible(false);
		model.loadComplaintsTableModel();
	}

	@Override
	public void ancestorMoved(AncestorEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ancestorRemoved(AncestorEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
		//if(e.getValueIsAdjusting())
		ListSelectionModel lsm = (ListSelectionModel)e.getSource();
		JTable table = this.view.getComplaintsForm().getComplaintsTable().getTable();
		int row = table.getSelectedRow();
		if(e.getValueIsAdjusting()==false){			
			//System.out.println(table.getValueAt(lsm.getMinSelectionIndex(),3)+" "+e.getValueIsAdjusting());
			this.view.getComplaintsForm().getTxtComplaintTitle().setText(table.getValueAt(row, 4).toString());
			this.view.getComplaintsForm().getTextDetails().setText(table.getValueAt(row, 1).toString());
			if(!this.view.getComplaintsForm().getComplaintPane().isVisible())
				 this.view.getComplaintsForm().getComplaintPane().setVisible(true);		   
			this.view.getComplaintsForm().setCompID(Integer.parseInt(table.getValueAt(row, 0).toString()));
		}
	}

	@Override
	public void tableChanged(TableModelEvent e) {
		// detect if checkbox pressed
		if(e.getType()==TableModelEvent.UPDATE){
			DefaultTableModel tm = this.view.getComplaintsForm().getComplaintsTable().getDtm();
			boolean changedVal = Boolean.parseBoolean(tm.getValueAt(e.getFirstRow(), 5).toString());
			Complaints comp = new Complaints();
			comp.setComplaintID(Integer.parseInt(tm.getValueAt(e.getFirstRow(), 0).toString()));
			
			if(changedVal==true){
				comp.setStatus(1);
				model.updateResolvedStatus(comp);
			}else if(changedVal==false){
				comp.setStatus(0);
				model.updateResolvedStatus(comp);
			}
		}
	}

}

